package com.example.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.Map;
import java.util.Objects;

public interface BaseMapperPlus extends BaseMapper{

    @Select({"<script>" +
            "   SELECT * FROM ${tableName} WHERE user_id = #{userId} and is_deleted='0' order by create_time desc" +
            "</script>"})
    IPage<Map> pageData(@Param("tableName")String tableName, @Param("userId")String userId,Page<Map> page);

    @Select("SELECT * FROM ${tableName} WHERE  uuid=#{uuid} and is_deleted='0'")
    Map getDataByUUID(@Param("tableName")String tableName, @Param("uuid")String uuid);

    @Select({"<script>" +
            "SELECT * FROM ${tableName} WHERE user_id = #{userId} and is_deleted='0'" +
            "<if test='params != null and params.size() > 0'>" +
            "   <foreach collection='params' index='key' item='value' open=' AND (' separator=' AND ' close=')'>" +
            "       <choose>" +
            "           <when test='value == null'>" +
            "               ${key} IS NULL" +
            "           </when>" +
            "           <when test='value instanceof java.util.List or value instanceof java.util.ArrayList'>" +
            "               ${key} IN " +
            "               <foreach collection='value' item='item' open='(' separator=',' close=')'>" +
            "                   #{item}" +
            "               </foreach>" +
            "           </when>" +
            "           <otherwise>" +
            "               ${key} = #{value}" +
            "           </otherwise>" +
            "       </choose>" +
            "   </foreach>" +
            "</if>" +
            " ORDER BY create_time DESC" +
            "</script>"})
    public  IPage<Map> pageData(@Param("tableName") String tableName,
                                 @Param("userId") String userId,
                                 @Param("params") Map<String, Object> params,
                                 Page<Map> page);

}
